package com.chart.web.controller.chart;
import com.chart.chart.domian.ChartTheme;
import com.chart.chart.domian.ChartType;
import com.chart.chart.domian.vo.*;
import com.chart.chart.service.IChartThemeService;
import com.chart.chart.service.impl.ChartThemeServiceImpl;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.core.domain.entity.ChartList;
import com.chart.common.core.domain.entity.ChartThemeType;
import com.chart.system.service.ChartListService;
import com.chart.system.service.ChartThemeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/chart")
public class ChartListController {

    @Autowired
    private ChartListService chartListService;

    @Autowired
    private IChartThemeService chartThemeService;

    @Autowired
    private ChartThemeTypeService chartThemeTypeService;
    /**
     * 获取图表列表接口
     * @return
     */
   // @GetMapping("/list")
    public AjaxResult list() {
        List<ChartListsVO> chartLists = chartListService.getChartsByStatus(); // 查询上线状态的图表并转换为 VO
        return AjaxResult.success(chartLists);
    }






    @GetMapping("/list")
    public AjaxResult lists() {
        List<ChartTypeVO> chartLists = chartListService.getChartsByStatuss();
        return AjaxResult.success(chartLists);
    }






    /**
     * 获取图表详情接口
     * @param chartCode
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam Integer chartCode) {
        if (chartCode == null) {
            return AjaxResult.error(400, "参数为空，请重试!");
        }

        // 获取图表详情，返回的是 ChartDetailVO 类型
        ChartDetailVO chartDetail = chartListService.getChartDetail(chartCode);

        if (chartDetail == null) {
            return AjaxResult.error(404, "图例不存在");
        }

        // 返回成功结果，包含图表详情
        return AjaxResult.success(chartDetail);
    }

















    @GetMapping("/themes")
    public AjaxResult getThemes() {
        try {
            // 获取类型列表和主题列表
            List<ChartThemeType> typeList = chartThemeTypeService.getChartThemeTypes();
            List<ChartTheme> themeList = chartThemeService.getChartThemes();
            // 转换为 VO 对象
            List<TypeVO> typeVOList = typeList.stream()
                    .map(type -> {
                        TypeVO typeVO = new TypeVO();
                        typeVO.setId(type.getId().longValue());  // 确保 id 是 Long 类型
                        typeVO.setTypeName(type.getTypeName());
                        typeVO.setIsDefault(type.getIsDefault() != null ? type.getIsDefault().intValue() : 0); // 确保返回 0 或 1
                        return typeVO;
                    })
                    .collect(Collectors.toList());
            List<ThemeVO> themeVOList = themeList.stream()
                    .map(theme -> {
                        ThemeVO themeVO = new ThemeVO();
                        themeVO.setId(theme.getId().longValue());  // 确保 id 是 Long 类型
                        themeVO.setThemeValue(theme.getThemeValue());
                        themeVO.setThemeType(theme.getThemeType());  // theme_type 使用 Integer 类型
                        themeVO.setIsDefault(theme.getIsDefault() != null && theme.getIsDefault() ? 1 : 0); // 转换 Boolean 为 0 或 1
                        return themeVO;
                    })
                    .collect(Collectors.toList());


            // 构建返回数据
            ThemeResponseVO response = new ThemeResponseVO();
            response.setType(typeVOList);
            response.setTheme(themeVOList);

            // 返回成功响应
            return AjaxResult.success("成功", response);
        } catch (Exception e) {
            // 返回错误响应
            return AjaxResult.error("失败: " + e.getMessage());
        }
    }










}
